<?php

class FIL {
  var $conn;

  function __construct( $servername, $username, $password, $dbname){
    $this->conn = new mysqli($servername, $username, $password, $dbname);
    if ($this->conn->connect_error){
      $this->conn = -1;
      return;
    }
    $this->conn->set_charset("utf8");
  }

  function __destruct(){
    $this->conn->close();
  }

  function add($parent, $name, $link, $uid){
    $stmtdir = $this->conn->prepare('INSERT INTO bkmk_fil ( parent, name, link, uid ) VALUES ( ?, ?, ?, ? )');

    $stmtdir->bind_param('issi', $parent, $name, $link, $uid);
    $stmtdir->execute();

    $stmtdir->close();

    return true;
  }

  function alt($id, $parent, $name, $link, $uid){
    $stmtdir = $this->conn->prepare('UPDATE bkmk_fil SET parent = ?, name = ?, link = ?, uid = ?, time = NOW() WHERE id = ?');

    $stmtdir->bind_param('issii', $parent, $name, $link, $uid, $id);
    $stmtdir->execute();

    $stmtdir->close();

    return true;
  }

  function getById($id){
    $stmtdir = $this->conn->prepare('SELECT id, parent, name, link, uid FROM bkmk_fil WHERE id = ?');

    $stmtdir->bind_param('i', $id);
    $stmtdir->execute();
    $stmtdir->store_result();
    $stmtdir->bind_result($rid, $rparent, $rname, $rlink, $ruid);

    if($stmtdir->fetch()){

      $data = array();
      $data['id']     = $rid;
      $data['parent'] = $rparent;
      $data['name']   = $rname;
      $data['link']   = $rlink;
      $data['uid']    = $ruid;

    }else{

      $data = false;

    }

    $stmtdir->close();

    return $data;
  }

  function delById($id){
    $stmtdir = $this->conn->prepare('DELETE FROM bkmk_fil WHERE id = ?');

    $stmtdir->bind_param('i', $id);
    $stmtdir->execute();

    $stmtdir->close();

    return true;
  }
}
